package edu.syr.iis.web.controller;

import java.beans.PropertyEditorSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.syr.iis.web.model.Adjacency;
import edu.syr.iis.web.model.AdjacencyManager;

public class AdjacencyPropertyEditor extends PropertyEditorSupport {
	
	AdjacencyManager manager = null;	
	private static Log log = LogFactory.getLog(AdjacencyPropertyEditor.class);	
	
	public AdjacencyPropertyEditor(AdjacencyManager manager) {
		super();
		this.manager = manager;
	}


	@Override
	public void setAsText(String text)  throws IllegalArgumentException 
	{
		
		log.debug("Entering: setAsText() with: " + text);
		
		if (text.equals("null"))
		{
			setValue(null);
		} 
		else
		{
			int id = Integer.parseInt(text);
			Adjacency value = manager.getAdjacency(id);

			if (value == null) {
				log.debug("No value object with id: " + id);
				throw new IllegalArgumentException("No value object with id: " + id);
			}
			setValue(value);
		}
	}
	
	@Override
	public String getAsText() {			
	 
		Adjacency value = (Adjacency) getValue();
		String text = "null";
		
		log.debug("Entering getAsText() with " + value);

		if (value != null)
			text = Integer.toString(value.getAdjacencyId());
		
		log.debug("Returning: " + text);
		return text;
	}

	

}
